package com.inspector.app.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.Map;

public class SharedPreferencesUtils {
    private static Context mContext;

    public SharedPreferencesUtils() {
    }

    public static void init(Context context) {
        if (null == context) {
            throw new IllegalArgumentException("context cannot be null.");
        } else {
            mContext = context.getApplicationContext();
        }
    }

    private static SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    private static SharedPreferences getSharedPreferences(String name) {
        if (null == mContext) {
            throw new IllegalStateException("Please invoke init method first.");
        } else {
            SharedPreferences sharedPreferences;
            if (StringUtils.isEmpty(name)) {
                sharedPreferences = getSharedPreferences();
            } else {
                sharedPreferences = mContext.getSharedPreferences(name, 0);
            }

            return sharedPreferences;
        }
    }

    public static boolean put(String key, Object value) {
        return put((String)null, key, value);
    }

    public static boolean put(String name, String key, Object value) {
        if (!StringUtils.isEmpty(key) && null != value) {
            SharedPreferences.Editor editor = getSharedPreferences(name).edit();
            if (value instanceof String) {
                editor.putString(key, String.valueOf(value));
            } else if (value instanceof Boolean) {
                editor.putBoolean(key, Boolean.parseBoolean(value.toString()));
            } else if (value instanceof Float) {
                editor.putFloat(key, (Float)value);
            } else if (value instanceof Integer) {
                editor.putInt(key, (Integer)value);
            } else if (value instanceof Long) {
                editor.putLong(key, (Long)value);
            }

            return editor.commit();
        } else {
            throw new RuntimeException("key or value cannot be null.");
        }
    }

    public static boolean putAll(String key, List<?> list) {
        return putAll((String)null, key, list);
    }

    public static boolean putAll(String name, String key, List<?> list) {
        if (!StringUtils.isEmpty(key) && !CollectionUtils.isEmpty(list)) {
            SharedPreferences.Editor editor = getSharedPreferences(name).edit();
            int size = list.size();
            int i;
            if (list.get(0) instanceof String) {
                for(i = 0; i < size; ++i) {
                    editor.putString(key + i, (String)list.get(i));
                }
            } else if (list.get(0) instanceof Long) {
                for(i = 0; i < size; ++i) {
                    editor.putLong(key + i, (Long)list.get(i));
                }
            } else if (list.get(0) instanceof Float) {
                for(i = 0; i < size; ++i) {
                    editor.putFloat(key + i, (Float)list.get(i));
                }
            } else if (list.get(0) instanceof Integer) {
                for(i = 0; i < size; ++i) {
                    editor.putLong(key + i, (long)(Integer)list.get(i));
                }
            } else if (list.get(0) instanceof Boolean) {
                for(i = 0; i < size; ++i) {
                    editor.putBoolean(key + i, (Boolean)list.get(i));
                }
            }

            return editor.commit();
        } else {
            throw new RuntimeException("key or list cannot be null.");
        }
    }

    public static Map<String, ?> getAll() {
        return getAll((String)null);
    }

    public static Map<String, ?> getAll(String name) {
        return getSharedPreferences(name).getAll();
    }

    public static boolean getBoolean(String key) {
        return getBoolean((String)null, key);
    }

    public static boolean getBoolean(String name, String key) {
        return getSharedPreferences(name).getBoolean(key, false);
    }

    public static long getLong(String key) {
        return getLong((String)null, key);
    }

    public static long getLong(String name, String key) {
        return getSharedPreferences(name).getLong(key, 0L);
    }

    public static float getFloat(String key) {
        return getFloat((String)null, key);
    }

    public static float getFloat(String name, String key) {
        return getSharedPreferences(name).getFloat(key, 0.0F);
    }

    public static int getInt(String key) {
        return getInt((String)null, key);
    }

    public static int getInt(String name, String key) {
        return getSharedPreferences(name).getInt(key, 0);
    }

    public static int getInt(String name, String key, int defaultValue) {
        return getSharedPreferences(name).getInt(key, defaultValue);
    }

    public static String getString(String key) {
        return getString((String)null, key);
    }

    @Nullable
    public static String getString(String name, String key) {
        return getSharedPreferences(name).getString(key, (String)null);
    }



    public static boolean remove(String key) {
        return remove((String)null, key);
    }

    public static boolean remove(String name, String key) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.remove(key);
        return editor.commit();
    }

    public static boolean clear() {
        return clear((String)null);
    }

    public static boolean clear(String name) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.clear();
        return editor.commit();
    }
}

